//
//  QuickCardController.m
//  QRMaster
//
//  Created by myApple on 16/5/26.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "QuickCardController.h"
#import "QuickCardProcess.h"
#import "QuickCardViewCell.h"
#import "EditShareTimeViewController.h"
#import "ReaderQRCodeViewController.h"
#import "AddQuickCardController.h"
#import "EditQuickCardController.h"
#import "EditQuickCardController.h"
#import "CardInfoModel.h"
#import "GenerateQRCardController.h"
#import "NoInfoPromtView.h"
#import "ShareTimeEditVC.h"
@interface QuickCardController ()<UITableViewDataSource,UITableViewDelegate,QuickCardViewCellDelegate,NoInfoPromtViewDelegate>
{
    QuickCardProcess * cardProcess;
    UIButton * readerQRCodeBtn;
    NoInfoPromtView * noInfoPromtView;
}

@property(nonatomic,strong)NSArray * collectCardArray;
@property(nonatomic,strong)UITableView * collectCardTableView;
@end

@implementation QuickCardController

#pragma mark - lifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    cardProcess = [[QuickCardProcess alloc]init];
    [self prepareTableView];
    [self prepareNavBtn];
    [self initData];
    [self prepareNoDataView];
    // Do any additional setup after loading the view.
    self.title = NSLocalizedString(@"qiuckCard", nil);
    self.view.backgroundColor = UIColorFromRGB(0xebebeb);
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [cardProcess getCollectCardByPlist];
//    [cardProcess getFaveriterCard];
     __weak QuickCardController * weakSelf = self;
    cardProcess.collectCardBlcok = ^(NSArray * cardArray)
    {
        _collectCardArray = cardArray;
        [weakSelf.collectCardTableView reloadData];
    };
//    _collectCardArray = @[];
    if (_collectCardArray.count==0) {
        noInfoPromtView.hidden = NO;
    }
    readerQRCodeBtn.hidden = NO;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    readerQRCodeBtn.hidden = YES;
}


#pragma mark - initData
-(void)initData
{
    __weak QuickCardController * weakSelf = self;
    [cardProcess getFaveriterCard];
    cardProcess.collectCardBlcok = ^(NSArray * cardArray)
    {
        _collectCardArray = cardArray;
        [weakSelf.collectCardTableView reloadData];
    };
//    _collectCardArray = @[];
    if (_collectCardArray.count==0) {
        noInfoPromtView.hidden = NO;
    }

}

- (void)prepareNoDataView{
    float x = 0;
    float y = 0;
    float width = UI_SCREEN_WIDTH;
    float height = self.view.frame.size.height;
    noInfoPromtView = [[NoInfoPromtView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    //    [promtView prepareViewWithPromtTitle:NSLocalizedString(@"noFunctionCard", nil)];
    [noInfoPromtView prepareViewWith:@"noinformation_dark" withPromt:NSLocalizedString(@"noRoomInfo", nil)];
    noInfoPromtView.hidden = YES;
    noInfoPromtView.delegate = self;
    [self.view addSubview:noInfoPromtView];
    
}

#pragma mark - preparView
-(void)prepareTableView
{
    _collectCardTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT - 44 - 64 - 49) style:UITableViewStylePlain];
    _collectCardTableView.delegate = self;
    _collectCardTableView.dataSource = self;
    _collectCardTableView.backgroundColor = UIColorFromRGB(0xebebeb);
    [self.view addSubview:_collectCardTableView];
    _collectCardTableView.separatorStyle = NO;
    _collectCardTableView.tableFooterView = [UIView new];
    
}

-(void)prepareNavBtn
{
    readerQRCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    readerQRCodeBtn.frame = CGRectMake(UI_SCREEN_WIDTH - 48, 0, 48, 44);
    [readerQRCodeBtn addTarget:self action:@selector(readQRCode) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationController.navigationBar addSubview:readerQRCodeBtn];
    
    UIImageView * readerQRCodeImgView = [[UIImageView alloc]initWithFrame:CGRectMake(16, 15, 16, 15)];
    readerQRCodeImgView.image = [UIImage imageNamed:@"scan_code"];
    [readerQRCodeBtn addSubview:readerQRCodeImgView];
}

#pragma mark - 扫一扫
-(void)readQRCode
{
    ReaderQRCodeViewController * rvc = [[ReaderQRCodeViewController alloc]init];
    rvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:rvc animated:YES];
}

#pragma mark - tableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _collectCardArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 193;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_collectCardArray.count>indexPath.row) {
        QuickCardModel * model = _collectCardArray[indexPath.row];
        QuickCardViewCell * cell = [QuickCardViewCell cellWithTableView:tableView];
        cell.index = indexPath.row;
        cell.delegate = self;
        cell.backgroundColor = UIColorFromRGB(0xebebeb);
        [cell setData:model];
        return cell;
    }else{
        QuickCardViewCell * cell = [QuickCardViewCell cellWithTableView:tableView];
        return cell;
    }
  
    
}

#pragma mark - quickCardViewCellDelegate
-(void)QuickCardViewCellAddNewRoom:(NSInteger)index
{
    AddQuickCardController * aqcc = [[AddQuickCardController alloc]init];
    aqcc.hidesBottomBarWhenPushed = YES;
    aqcc.index = index;
    [self.navigationController pushViewController:aqcc animated:YES];
}

-(void)QuickCardViewCellEidt:(QuickCardModel *)currentModel
{
    EditQuickCardController * eqcc = [[EditQuickCardController alloc]init];
    eqcc.hidesBottomBarWhenPushed = YES;
    eqcc.model = currentModel;
    [self.navigationController pushViewController:eqcc animated:YES];
}

-(void)QuickCardViewCellShare:(QuickCardModel *)currentModel
{
//    EditShareTimeViewController * estvc = [[EditShareTimeViewController alloc]init];
    ShareTimeEditVC * estvc = [[ShareTimeEditVC alloc] init];
    estvc.communityId = currentModel.communityId;
    estvc.roomId = currentModel.roomId;
    estvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:estvc animated:YES];
}

-(void)QuickCardViewCellTop:(QuickCardModel *)currentModel
{
     __weak QuickCardController * weakSelf = self;
    [cardProcess sortArrayIn:[NSMutableArray arrayWithArray:_collectCardArray] by:currentModel];
    cardProcess.collectCardBlcok = ^(NSArray * cardArray)
    {
        _collectCardArray = cardArray;
        [weakSelf.collectCardTableView reloadData];
    };
}

-(void)QuickCardViewCellGenerateCard:(QuickCardModel *)model
{
    CardInfoModel * cardModel = [[CardInfoModel alloc]init];
    cardModel.roomId = model.roomId;
    cardModel.roomName = model.roomName;
    cardModel.bulidingId = model.buildingId;
    cardModel.bulidingName = model.buildingName;
    cardModel.floorId = model.floorId;
    cardModel.floorName = model.floorName;
    cardModel.communityid = model.communityId;
    GenerateQRCardController * gcvc = [[GenerateQRCardController alloc]initWithCardModel:cardModel];
    gcvc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:gcvc animated:YES];
}

-(void)clickRefresh{
    DDLOG(@"刷新");
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
